#define DIRECTINPUT_VERSION 0x0800

#include "CameraInputComponent.h"
#include <dinput.h>

BYTE CameraInputComponent::bindings[11] = {DIK_W, DIK_S, DIK_D, DIK_A, DIK_Z, DIK_X, DIK_R};
IEngineComponentRegistry cameraInputComponentRegistery("CameraInputComponent", CameraInputComponent::Deserialize);

CameraInputComponent::CameraInputComponent()
{
	this->cameraId = -1;
}

CameraInputComponent::CameraInputComponent(long long cameraId)
{
	this->cameraId = cameraId;
}

CameraInputComponent::~CameraInputComponent(void)
{
}

void CameraInputComponent::setCameraId(long long int cameraId)
{
	this->cameraId = cameraId;
}

void CameraInputComponent::getHoldBindings(BYTE** bindings, DWORD* nBindings)
{
	*bindings = this->bindings;
	*nBindings = 7;
}

void CameraInputComponent::keyHold(BYTE key, LONGLONG timeElapsed)
{
	switch (key)
	{
		case DIK_W:
			camera->setCameraArc(camera->getCameraArc() + (.0001f * timeElapsed));
			break;
		case DIK_S:
			camera->setCameraArc(camera->getCameraArc() - (.0001f * timeElapsed));
			break;
		case DIK_D:
			camera->setCameraRotation(camera->getCameraRotation() + (.0001f * timeElapsed));
			break;
		case DIK_A:
			camera->setCameraRotation(camera->getCameraRotation() - (.0001f * timeElapsed));
			break;
		case DIK_Z:
			camera->setCameraDistance(camera->getCameraDistance() + (.00025f * timeElapsed));
			break;
		case DIK_X:
			camera->setCameraDistance(camera->getCameraDistance() - (.00025f * timeElapsed));
			break;
		case DIK_R:
			camera->setCameraArc(-30);
			camera->setCameraRotation(0);
			camera->setCameraDistance(100);
			break;
	}
}

void CameraInputComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "CameraInputComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("cameraId", this->cameraId);
	context->EndItem(name);
}

IEngineComponent* CameraInputComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	CameraInputComponent* input = new CameraInputComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			input->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "cameraId") == 0)
		{
			input->cameraId = context->ChildValueAsULongLong(index);
		}
		childName = context->ChildName(++index);
	}
	
	return input;
}

void CameraInputComponent::Initialize(EnginePointer<IEngineComponentCollection> components)
{
	camera = (EnginePointer<CenterCameraComponent>)components->getComponentFromId(cameraId);
}